let gulp= require('gulp');

// 1、打包css
let cssmin = require('gulp-cssmin');
// css自动添加兼容的前缀，虽然没要求，拿来练手
let autoprefixer = require('gulp-autoprefixer');

//没有sass，所以不打包

// 2、打包js
let uglify = require('gulp-uglify')
// es6转es5的，需要三个包，引入一个就可以
let babel = require('gulp-babel')


// 3、打包html
//没用组件，所以没下载组件的包
let htmlmin = require('gulp-htmlmin')

// 删除第三方
let del = require('del')





// 1、打包css
function cssHandler() {
    return gulp
    .src('./JD/css/*.css')
    .pipe(autoprefixer())
    .pipe(cssmin())
    .pipe(gulp.dest('./dist/css/'))
}

// 2、打包js
function jsHandler() {
    return gulp
    .src('./JD/js/**.*')
    .pipe(babel({presets:['@babel/preset-env']}))
    .pipe(uglify())
    .pipe(gulp.dest('./dist/js/'))
}
function swiperHandler() {
    return gulp
    .src('./JD/js/swiper/**.*')
    .pipe(gulp.dest('./dist/js/swiper/'))
}

// 3、打包html
function htmlHandler() {
    return gulp
    .src('./JD/*.html')
    // .pipe(htmlmin({
    //     removeComments: true, //  是否移除注释
    //     removeEmptyAttributes: true, // 是否移除空属性
    //     removeAttributeQuotes: true,  // 是否移除属性值的双引号
    //     collapseWhitespace: true, //是否移除空格和换行
    //     collapseBooleanAttributes: true, // 是否简化属性值为布尔的属性（`checked="checked"`）
    //     removeScriptTypeAttributes: true, //是否移除 script 标签的默认 type 属性
    //     removeStyleLinkTypeAttributes: true, // 是否移除 link 和 style 标签的默认 type 属性
    //     minifyCSS: true, // 是否压缩页面中给的 style 标签（内嵌式 css代码）
    //     minifyJS: true, // 是否压缩页面中的 script 标签（内嵌式 js 代码）
    // }))
    .pipe(gulp.dest('./dist/'))
}

// 4、打包图片
function imgHandler(){
    return gulp
    .src('./JD/images/**.*')
    .pipe(gulp.dest('./dist/images/'))
}

// 5、打包其余的
function fontsHandler(){
    return gulp
    .src('./JD/fonts/**.*')
    .pipe(gulp.dest('./dist/fonts/'))
}

// 内有其余文件夹，下面一起打包
function icoHandler(){
    return gulp
    .src('./JD/icomoon/**.*')
    .pipe(gulp.dest('./dist/icomoon/'))
}
function icoDemoHandler(){
    return gulp
    .src('./JD/icomoon/demo-files/**.*')
    .pipe(gulp.dest('./dist/icomoon/demo-files/'))
}
function icoFontsHandler(){
    return gulp
    .src('./JD/icomoon/fonts/**.*')
    .pipe(gulp.dest('./dist/icomoon/fonts/'))
}

function libHandler(){
    return gulp
    .src('./JD/lib/**.*')
    .pipe(gulp.dest('./dist/lib/'))
}
function libLayerHandler(){
    return gulp
    .src('./JD/lib/layer/**.*')
    .pipe(gulp.dest('./dist/lib/layer/'))
}
function libLayerHandler(){
    return gulp
    .src('./JD/lib/layer/**.*')
    .pipe(gulp.dest('./dist/lib/layer/'))
}
function libLayerMobileHandler(){
    return gulp
    .src('./JD/lib/layer/mobile/**.*')
    .pipe(gulp.dest('./dist/lib/layer/mobile/'))
}
function libLayerMobileNeedHandler(){
    return gulp
    .src('./JD/lib/layer/mobile/need/**.*')
    .pipe(gulp.dest('./dist/lib/layer/mobile/need/'))
}
function libLayerThemeHandler(){
    return gulp
    .src('./JD/lib/layer/theme/default/**.*')
    .pipe(gulp.dest('./dist/lib/layer/theme/default/'))
}



function uploadsHandler(){
    return gulp
    .src('./JD/uploads/**.*')
    .pipe(gulp.dest('./dist/uploads/'))
}
function faviconHandler(){
    return gulp
    .src('./JD/*.ico')
    .pipe(gulp.dest('./dist/'))
}

// 打包package.json
function packageHandler(){
    return gulp
    .src('./*.json')
    .pipe(gulp.dest('./dist/'))
}

// 配置删除任务
function delHandler(){
    return del(["./dist/"])
}

// 配置综合任务
let _default = gulp.series(
    delHandler,
    gulp.parallel(
        cssHandler,
        jsHandler,
        swiperHandler,
        htmlHandler,
        imgHandler,
        fontsHandler,
        icoHandler,
        icoDemoHandler,
        icoFontsHandler,
        uploadsHandler,
        faviconHandler,
        libHandler,
        libLayerHandler,
        libLayerMobileHandler,
        libLayerMobileNeedHandler,
        libLayerThemeHandler,
        packageHandler
    )
)


module.exports = {
    default : _default
}